﻿using System;
using System.Drawing.Printing;
using System.IO;
using System.IO.Packaging;
using System.Windows.Media;
using System.Windows.Xps;
using System.Windows.Xps.Serialization;
using System.Windows.Xps.Packaging;
using System.IO.Compression;
using System.Windows.Documents;


namespace Helper
{
    public partial class PrintHelperBase
    {
         FixedDocument fd;

        public PrintHelperBase()
        {            
            
        }
        public PrintHelperBase(object printObject)
        {
            
        }

        public FixedDocument FixedDocSeq
        {
            get { return fd; }
            protected set { fd = value; }
        }
        public static FixedDocumentSequence CreateDocFromVisual(object printObject)
        {
            MemoryStream ms = new MemoryStream();
            Package pkg = Package.Open(ms, FileMode.Create, FileAccess.ReadWrite);

            string packUri = "pack://stareheTemp.xps";
            PackageStore.AddPackage(new Uri(packUri), pkg);
            XpsDocument xpsDoc = new XpsDocument(pkg, System.IO.Packaging.CompressionOption.SuperFast, packUri);

            XpsDocumentWriter xpsWrriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
            VisualsToXpsDocument visualToXps = (VisualsToXpsDocument)xpsWrriter.CreateVisualsCollator();
            
            visualToXps.WriteAsync((Visual)printObject); 
            visualToXps.EndBatchWrite();

            FixedDocumentSequence xp = xpsDoc.GetFixedDocumentSequence();
            xpsDoc.Close();
            return xp;
        }

    }
}

      

          